//引入当前项目所需的所有包
var express = require('express');//引入express模块
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
require('./database/connect.js');//引入数据库连接模块
var  {checkToken}=require('./utils/tools.js');//引入token验证中间件
var cors =require('cors');//处理跨域
const { createProxyMiddleware } = require('http-proxy-middleware');//代理中间件 ;
require('./socket/socketServer.js')//引入websocket模块

//引入路由模块
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var furjiRouter = require('./routes/fanji');
var roleRouter = require('./routes/role');
var saleRouter = require('./routes/sale');
//创建express应用,创建服务对象
var app = express();
// const apiProxy = createProxyMiddleware({
//   target: 'https://www.zhipin.com',
//   changeOrigin: true,
//   pathRewrite: {'^/proxy' : ''},
//   on: {
//     proxyReq: (proxyReq, req, res) => { //自定义代理请求头相关配置 【请求伪装】
//       /* handle proxyReq */
//       proxyReq.setHeader('referer', 'https://www.zhipin.com/web/geek/job?');
//       proxyReq.setHeader('user-agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36 Edg/133.0.0.0');
//       proxyReq.setHeader('cookie','__zp_seo_uuid__=9dc0f06f-69b1-43ae-b7e2-5d2f6422cb99; __g=sem_bingpc; __l=r=https%3A%2F%2Fcn.bing.com%2F&l=%2Fwww.zhipin.com%2Fsem%2F10.html%3F_ts%3D1739411029866%26sid%3Dsem_bingpc%26qudao%3Dbing_pc_H120003UY5%26plan%3DBOSS-%25E5%25BF%2585%25E5%25BA%2594-%25E5%2593%2581%25E7%2589%258C%26unit%3D%25E7%25B2%25BE%25E5%2587%2586%26keyword%3Dboss%25E7%259B%25B4%25E8%2581%2598%26msclkid%3Daeda0690252317db2a16613d6b1ad4dd&s=1&g=%2Fwww.zhipin.com%2Fsem%2F10.html%3F_ts%3D1739411029866%26sid%3Dsem_bingpc%26qudao%3Dbing_pc_H120003UY5%26plan%3DBOSS-%25E5%25BF%2585%25E5%25BA%2594-%25E5%2593%2581%25E7%2589%258C%26unit%3D%25E7%25B2%25BE%25E5%2587%2586%26keyword%3Dboss%25E7%259B%25B4%25E8%2581%2598%26msclkid%3Daeda0690252317db2a16613d6b1ad4dd&s=3&friend_source=0; Hm_lvt_194df3105ad7148dcf2b98a91b5e727a=1739411031; HMACCOUNT=86797A53B93DE813; wbg=0; wt2=D5hoTB3ulSSSLMGhsSGeCDkEQ5vecwcYN1ZCkvQIPoeCeybHh7q5R0h-T2ku5MWgNiVV9ocefeoYPl2RikyS4Qg~~; zp_at=PdFBe3GboYtOP4yBt97CLMHGtIxbK7sOMf8XgRuhDT0~; ab_guid=c6d5d86d-cb79-4ca0-ad37-4868d8b93df7; Hm_lpvt_194df3105ad7148dcf2b98a91b5e727a=1739413464; __zp_stoken__=e736fw4nDpE4XOmQYGBEYY14OUX5af2TCvsOBYMOGXHfCiMKvYVJhwrFqwpzCvsKLZ8Kfwr7DhXbCl3jCmMKuwqhVwqHCm8KmVsO6YcSDVMKgxJzDqsOgxIDEssK2wrvCokAyDgsJCQ8MDQ8PCRcSDw8JFBUXFxEVFBISGDk3w7nCo047PUQ5LVVUUgtRZmZJaE0QYEpMPzoNZA5mOjE5OT8%2Bw4F5wr7Cl8OIe8K%2BwpHDhX7DgMOhOUc%2BO8K%2FKiwwwr5JDWgKwr4yDcK%2Fw4cPwrnDqFzDjmTCjHLDnMK5bTU%2FOcK%2BxLxEQB1IPkBCOjtDQEAtO2zDiWLCjnXDoMK%2BXSo%2BHkhAOkRAQEA6Qj5CLDpDEypAPy5CGA0XFxAvQ8K7w4HCu8OhQDo%3D; bst=V2RNMuEuz03FxrVtRuyx0aLCu47DrRwSU~|RNMuEuz03FxrVtRuyx0aLCu47DrWxi4~; __c=1739411030; __a=39466898.1711069159.1712564609.1739411030.6.3.4.4')
//     },
//     proxyRes: (proxyRes, req, res) => {
//       /* handle proxyRes */
//     }
//   }, 
// });
const apiProxy1 = createProxyMiddleware({
  target: 'https://m.maizuo.com',
  changeOrigin: true,
  pathRewrite: {'^/proxy' : ''},
  on: {
    proxyReq: (proxyReq, req, res) => { //自定义代理请求头相关配置 【请求伪装】
      /* handle proxyReq */
      proxyReq.setHeader('x-client-info','{"a":"3000","ch":"1002","v":"5.2.1","e":"17389815294305249447706625"}');
      proxyReq.setHeader('x-host', 'mall.film-ticket.film.list');
      proxyReq.setHeader('x-requested-with',`XMLHttpRequest`)
    },
    proxyRes: (proxyRes, req, res) => {
      /* handle proxyRes */
    }
  }, 
});

//中间件(提供特有的能力,转接头),为服务对象配置中间件
app.use(cors());//开放跨域
// app.use('/proxy',apiProxy);
app.use('/proxy',apiProxy1);
app.use(logger('dev'));
app.use(express.json());//解析请求体数据,处理客户端提交的的json数据
app.use(express.urlencoded({ extended: false }));
app.use(express.json({limit:'5000mb'}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));//引导客户端访问静态资源

//路由接口集成到app应用对象中
app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/fanji', (req,res,next)=>{
  checkToken(req,res,next);
},furjiRouter);
app.use('/role', roleRouter);
app.use('/sale', saleRouter);
//对外抛出app服务对象
module.exports = app;
